package com.wing.cabinet.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nimbusds.jwt.JWT;
import com.wing.cabinet.dao.UseInventoryUserKnifeDao;
import com.wing.cabinet.model.entity.UseInventoryUserKnife;
import com.wing.cabinet.model.request.UseInventoryUserKnifeForm;
import com.wing.cabinet.model.response.UseInventoryInoutVO;
import com.wing.cabinet.model.response.UseInventoryUserKnifeVO;
import com.wing.common.utils.LocalDateTimeUtils;
import com.wing.product.model.entity.ProductDetail;
import com.wing.product.service.ProductDetailService;
import com.wing.product.service.ProductTypeService;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Stream;

/**
 * 刀具编号-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class UseInventoryUserKnifeServiceImpl extends ServiceImpl<UseInventoryUserKnifeDao, UseInventoryUserKnife> implements UseInventoryUserKnifeService {


    @Autowired
    private ProductDetailService productDetailService;

    @Autowired
    private ProductTypeService productTypeService;

    @Override
    public int add(UseInventoryUserKnifeForm useInventoryUserKnifeForm) {

        //校验

        UseInventoryUserKnife useInventoryUserKnife =new UseInventoryUserKnife();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(UseInventoryUserKnife.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(useInventoryUserKnifeForm, useInventoryUserKnife, copyOptions);

        return baseMapper.insert(useInventoryUserKnife);
    }

    @Override
    public List<UseInventoryUserKnifeVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<UseInventoryUserKnifeVO> pageList4app(Page page, Map<String, Object> params) {
        List<UseInventoryUserKnifeVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public UseInventoryUserKnifeVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public int updateKnife(UseInventoryUserKnife useInventoryUserKnife) {
        return baseMapper.updateKnife(useInventoryUserKnife);
    }


    @Override
    public IPage<UseInventoryUserKnifeVO> pageList(Page page, Map<String, Object> params) {
        List<UseInventoryUserKnifeVO> list = baseMapper.pageList(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<String> listByParentCodes(List<String> parentCodes) {
        return baseMapper.listByParentCodes(parentCodes);
    }

    @Override
    public List<UseInventoryUserKnifeVO> getNoRepayKnife(List<UseInventoryUserKnifeVO> records) {

        List<UseInventoryUserKnifeVO>list = new ArrayList<>();
        // records数据为领取未还的刀具    需要判断领取刀具类型的归还阈值为多少  超出则产生一条记录
        // 归还阈值
        if (!records.isEmpty()){
            for (int i = 0; i < records.size(); i++) {
                // 产品类型的归还时间
                Integer threshold = baseMapper.getThreshold(records.get(i).getSkuCode());
                // 产品的归还时间
                Integer returnThreshold = baseMapper.getReturnThreshold(records.get(i).getSkuCode());
                if (threshold==null&&returnThreshold==null){
                    continue;
                }
                LocalDateTime outTime = null;
                // 优选选择产品的归还阈值其次产品类型
                if (returnThreshold != null){
                    // 领取时间+归还时间
                     outTime = records.get(i).getOutTime().plusHours(returnThreshold);
                }
                if(returnThreshold!=null){
                    // 领取时间+归还时间
                    outTime = records.get(i).getOutTime().plusHours(threshold);
                }

                LocalDateTime now = LocalDateTime.now();
                // 当前时间比较领取时间+归还时间
                Duration between = Duration.between(now,outTime);
                long compare = between.toHours();
                if (compare<0){
                    records.get(i).setStatus(UseInventoryUserKnife.Status.used);
//                    records.get(i).setStatusDesc("");
                    list.add(records.get(i));
                }
            }
        }
        return list;
    }



    public static void main(String[] args) throws ParseException {
        LocalDateTime now = LocalDateTime.now(); // 当前时间

        //  给LocalDateTime 赋值
        LocalDateTime start = LocalDateTime.of(2022,1,1,8,0,0);
        System.out.println("计算两个时间的差：");
        LocalDateTime end = LocalDateTime.of(2022,1,2,8,30,30);


        Duration between = Duration.between(start,end);
        long l = between.toHours();
        System.out.println(l);

        /*System.out.println(i);
        System.out.println(date);*/
    }
}
